class EventsController < ApplicationController
   before_filter :login_required
   
   def my_fb_events
    
    if session[:facebook_session]     
      @eids = {}
      @events = session[:facebook_session].user.events(:start_time => Time.now.utc.to_i)
      eids = @events.map {|i| i.eid.to_s}
      Event.find(:all, :conditions => [" eid IN (?)", eids]).each {|i| @eids[i.eid.to_s] = i.id }
      
    end
   end
  
   def new
    @page_title = I18n.t(params[:eid] ? 'event.new_title_fb' : 'event.new_title')
    @event = Event.new(:date => Date.today, :date_end => Date.today)
    @event.build_place    
    @event_photo = Photo.new
    
    @place = Place.new
    @place.build_location    
    @place_photo = Photo.new
    
    @human = Human.new    
    @human_photo = Photo.new
    
    if params[:eid] and session[:facebook_session]             
      begin
        event_fb = session[:facebook_session].user.events(:eids => params[:eid])      
      rescue Curl::Err::RecvError
         flash[:error] = "fb_import"
      end
      if event_fb.length > 0
        e = event_fb[0]
        @event.name = e.name.downcase
        @event.eid = params[:eid]
        @event.description = e.event_type+ "," + e.event_subtype + "<br />" + e.description
        @event.date = ((Time.now + (e.start_time.to_i - Time.now.utc.to_i).seconds) - 9.hours).to_date
         time_end =  Time.now + (e.end_time.to_i - Time.now.utc.to_i).seconds - 9.hours
         time_end -= 1.day if time_end.hour < 7        
        @event.date_end = time_end.to_date        
        #@event.place.name = e.location.to_s.downcase        
        @event_photo.data_url = e.pic_big
        #@event_location = e.venue['street'].to_s + " " + e.venue['city'].to_s + " " + 
         #                   e.venue['state'].to_s + " " + e.venue['country'].to_s
      end
    end    
    render :action => 'form'
  end
  
  def create
    @page_title = I18n.t('event.new_title')    
    @event = Event.new(:user => current_user)        
    @event.attributes = params[:event] if params[:event]
    @event.save if (success = @event && @event.valid?)            
    if success && @event.errors.empty?      
      #redirect_back_or_default('/')
      redirect_to "/" + @event.alias #+ @event.alias
    else      
      @event.build_place unless @event.place
      @event_photo = Photo.new
      
      @place = Place.new
      @place.build_location    
      @place_photo = Photo.new
      
      @human = Human.new    
      @human_photo = Photo.new
      render :action => 'form'
    end
  end
  
  def edit    
    @page_title = I18n.t('event.edit_title')
    @event = Event.find(:first, :conditions => ["id = ?", params[:id]])    
    @event_photo = Photo.new  
    
    @place = Place.new
    @place.build_location    
    @place_photo = Photo.new
    
    @human = Human.new    
    @human_photo = Photo.new
    
    if @event.is_rewritable(current_user)
      render :action => 'form'
    else
      redirect_back_or_default('/')
    end    
  end
  
  def update
    @page_title = I18n.t('event.edit_title')    
    @event = Event.find(:first, :conditions => ["id = ?", params[:id]])
    if @event and @event.is_rewritable(current_user)      
      @event.attributes = params[:event]    
      @event.save if (success = @event && @event.valid?)    
      if success && @event.errors.empty?
        redirect_to "/" + @event.alias
      else 
        @event_photo = Photo.new
        @place = Place.new        
        @place.build_location    
        @place_photo = Photo.new
        
        @human = Human.new    
        @human_photo = Photo.new
         render :action => 'form'
      end
    else
      redirect_to "/" + @event.alias
    end
  end
  
  def auto_complete_for_name
    events = []    
    if  !(params[:q].blank? and params[:date].blank?) 
      q = ""; params[:q].split(" ").each { |i|  q += " name like '%#{i}%' OR " }
      q = q[0, q.length - 4]      
      Event.find(:all, :include => {:place => :location}, 
                            :conditions => [q + " and date = ?",  Date.parse(params[:date])], :limit => 100).each { |i|
                              events << {:id => i.id, :name => i.name, :place => i.place.name, :location => i.place.location.full_address}}      
    end    
    render :json => events
  end
end
